<template>
    <div class="components-container">
        <div class="layui-inline">
            <label class="layui-form-label">报表类型</label>
            <div class="layui-input-inline">
                <el-select v-model="strType" palceholder="请选择报表类型">
                    <el-option v-for="item in strtypes" :key="item.text" :label="item.value" :value="item.text"></el-option>
                </el-select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">统计日期</label>  
            <el-date-picker v-if="strType == '4' ||strType == '0'" v-date v-model="strTime" type="date" placeholder="选择日期范围" ></el-date-picker>
            <el-date-picker v-if="strType == '3'" v-date v-model="strTime" type="year" placeholder="选择日期范围" ></el-date-picker>
            <el-date-picker v-if="strType == '1'" v-date v-model="strTime" type="month" placeholder="选择日期范围" ></el-date-picker>
            <el-date-picker v-if="strType == '0'" v-date v-model="strEndTime" type="date" placeholder="选择日期范围"></el-date-picker>
        </div>
        <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
        <el-button type="warning"  @click="btnRepeat" >重置</el-button>
        <div style="height:56px">
            <ul class="toolList">
                <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
                  <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
                </li>
                <li>
                  <el-button type="primary" :disabled="this.$store.state.isloading" @click="exportExcel">导出Excel</el-button>
                </li>
            </ul>
        </div>
        <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow" :hiddenColumn="hiddenColumn"></dataTable>
        <h1 style="text-align:right;margin-right:100px;color:red;">数量合计:{{nums}}，金额合计:{{message}}元</h1>
      
      <printmodal ref="printPdf"></printmodal>
      <storehouseSummaryMain ref="dialog" @refreshData="getData"></storehouseSummaryMain>
    </div>
</template>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}
.btnCenter {
  text-align: center;
}
</style>
<script>
import sweetModal from "@/components/sweetModal";
import dataTable from "@/components/Table/DataTable";
import apiBase from "@/api/apiBase.js";
import storehouseSummaryMain from "./storehouseSummaryMain";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { ORDER } from "@/const/orderConst.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import printmodal from "@/components/Pdf/Print";
import informationRegistrationVue from '../jointPlatform/informationRegistration.vue';


const BALANCE = "结存";
const INFO = '详情';
const ADD = "新增";
const COPY = "打印";
const allocationnum = "调拨汇总";
const QXHZ = "取消汇总";



export default {
  name: "arehousingManage",
  mixins: [manage],
  data() {
    return {
      strtypes: [
          {text: '1',value: '月报'},
          {text: '3',value: '年报'},
          {text: '4',value: '日报'},
          {text: '0',value: '自定义'},
      ],
      strType: '4',
      strTime: null,
      strEndTime: null,
      hiddenColumn: false,
      nums: 0,
      message: 0,
      OutDataExcel: [],
    };
  },
  methods: {
    exportExcel() {
      var header
      var startTime
      if (this.strType === '0'){
        startTime = this.strTime.toLocaleDateString();
        var endTime = this.strEndTime.toLocaleDateString();
        header = '入库汇总统计表' + '(' + startTime + '--' + endTime + ')';
      }else if(this.strType === '1'){
        startTime = this.strTime.toLocaleDateString();
        var times = startTime.slice(0,8);
        header = '入库汇总统计表' + '(' + times + ')';
      }else if(this.strType === '3'){
        startTime = this.strTime.toLocaleDateString();
        var times = startTime.slice(0,4);
        header = '入库汇总统计表' + '(' + times + ')';
      }else if(this.strType === '4'){
        startTime = this.strTime.toLocaleDateString();
        header = '入库汇总统计表' + '(' + startTime + ')';
      }
    
      if (this.dataList === null){
          this.$message.error("无导出数据！");
          return;
      }
      this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.OutDataExcel, header);
        this.downloadLoading = false;
      });
    },
    //获取收费信息
    getData() {
        let data = {
            PageIndex: this.pageOrder.currentPage,
            PageSize: this.pageOrder.pageSize,
            OrderBy: this.pageOrder.orderType,
            OrderByType: this.pageOrder.orderSort,
        };
      if(this.strType == '0' || this.strType == '4'){
        data.strTime = formatDate(
            this.strTime,
            "yyyyMMdd"
        );
      }else if(this.strType == '1'){
        data.strTime = formatDate(
            this.strTime,
            "yyyyMM"
        );
      }else if(this.strType == '3'){
        data.strTime = formatDate(
            this.strTime,
            "yyyy"
        );
      }
      if(this.strType == '0'){
        data.strEndTime = formatDate(
            this.strEndTime,
            "yyyyMMdd"
        );
      };
      data.strType = this.strType;
      apiBase.DoService(data, apiBase.getApiAdress().Depot.QuerySumDpi, res => {
        this.dataList = res.data.content;
        this.OutDataExcel = res.data.content;
        this.pageOrder.totalNum = res.data.content.RecordCount;
        var s = 0;//数量
        var money = 0;//金额
        for(var i in this.dataList){
          var num = Number(this.dataList[i].DPLNUM);
          var moneys = Number(this.dataList[i].DPLBUYPRICE);
          s = num + s;
          money = moneys + money;
        }
        this.nums = s;
        this.message = money;
        if(this.OutDataExcel != null){
          this.OutDataExcel.push({DPGNAME: "合计",DPLNUM: this.nums,DPLBUYPRICE: this.message});
        }
      });
    },
    //获取表头
    getheaderlist(){
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetSumDpiColumns,
        res => {
          this.headList = res.data.content;
        }
      )
    },
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
         case ADD:
          this.$refs.dialog.open(this.order);
          break;
        case INFO:
          if(this.currentRow.HSLTYPE === "A"){
            this.$refs.dialog.open(this.order,this.currentRow.HSLID);
          }else{
            this.$refs.dialog.open('调拨汇总详情',this.currentRow.HSLID);
          }
          break;
        case COPY:
          this.$refs.printPdf.open("打印出库汇总明细",this.currentRow.HSLID);
          break;
        case allocationnum:
          this.$refs.dialog.open(this.order);
          break;
        default:
      }
    },
    //查询条件重置
    btnRepeat() {
      this.strTime = new Date();
      // this.strTime.setDate(1);
      this.strEndTime = new Date();
      this.strType = '4';
      this.getData();
    },
    //按钮显隐
    btnShow(btn) {
      if(btn === allocationnum) return true;
      if(btn === ADD) return true;
      if(!this.currentRow) return false;
      if(btn === COPY) return true;
      if(btn === QXHZ) return true;
    },
   
  },
  components: {
    sweetModal,
    dataTable,
    printmodal,
    storehouseSummaryMain
  },
  activated() {
    this.getData();
  },
  mounted() {
  },
  created() {
    this.getheaderlist();
    this.strTime = new Date();
    // this.strTime.setDate(1);
    this.strEndTime = new Date();
  },
};
</script>

